﻿@using NWNT.Web.Models
@model IEnumerable<QuestionAreaModel>
@{
    ViewBag.Title = "QuestionAreaList";
}

<h2>QuestionArea</h2>
<div>
    <table>
         <thead>
             <tr>
                <td>Area</td>
                <td>Description</td>
                <td>Command</td>
            </tr>
        </thead>
        <tbody data-bind="foreach: questionAreas">
            <tr data-bind="click: $root.selectRow, css: $root.selectedRowNo() === $root.questionAreas.indexOf($data) ? 'selectedRow':'unSelectedRow'">
                <td data-bind="text: Area"></td>
                <td data-bind="text: Description"></td>
                <td><a data-bind="attr:{href:'../Home/QuestionArea?areaId='+QuestionAreaId}">Edit</a></td>
            </tr>
        </tbody>
    </table>
    <a href="javascript:void(0)" data-bind="click: moveUp, visible:$root.moveUpEnable">Move Up</a>
    <a href="javascript:void(0)" data-bind="click: moveDown, visible: $root.moveDownEnable">Move Down</a>

     <a href="javascript:void(0)" data-bind="click: save">Save</a>
</div>

<script type="text/javascript">
    
    var data=[@Html.Raw(Json.Encode(Model))];
    data=data[0];
    function QuestionAreaList(questionAreas) {
        self = this;
        self.questionAreas = ko.observableArray(questionAreas),
        self.selectedRowNo = ko.observable(-1),
        self.moveUp = function () {
            var itemToMoveUp = self.questionAreas()[self.selectedRowNo()];
            var itemToMoveDown = self.questionAreas()[self.selectedRowNo() - 1];
            self.questionAreas()[self.selectedRowNo() - 1] = itemToMoveUp;
            self.questionAreas()[self.selectedRowNo()] = itemToMoveDown;
            self.selectedRowNo(self.selectedRowNo() - 1);
            self.questionAreas.valueHasMutated();
        },
        self.moveUpEnable = ko.computed(function () {
            return self.selectedRowNo() > 0 && self.selectedRowNo() !== -1;
        }),
        self.moveDown = function () {
            var itemToMoveUp = self.questionAreas()[self.selectedRowNo() + 1];
            var itemToMoveDown = self.questionAreas()[self.selectedRowNo()];
            self.questionAreas()[self.selectedRowNo() + 1] = itemToMoveDown;
            self.questionAreas()[self.selectedRowNo()] = itemToMoveUp;
            self.selectedRowNo(self.selectedRowNo() + 1);
            self.questionAreas.valueHasMutated();
        },
        self.moveDownEnable = ko.computed(function () {
            return self.selectedRowNo() < self.questionAreas().length - 1 && self.selectedRowNo() !== -1;
        }),
        self.selectRow = function (context) {
            self.selectedRowNo(self.questionAreas().indexOf(context));
            return true;
        },
        self.save = function () {
            var items = self.questionAreas();
            for (var i = 0; i < items.length; i++) {
                items[i].Order = i + 1;
            }
            var url = '../api/Master/UpdateQuestionAreaOrder';
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(items),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (a, b, c) {
                    var test = '';
                },
                error: function (a, b, c) {
                    var test1 = '';
                }
            });
        };
    }
    var viewModel = new QuestionAreaList(data);
    ko.applyBindings(viewModel);
</script>




